package cn.sunzq.springdata.elasticsearch.demo.importdata.service.impl;

import cn.sunzq.springdata.elasticsearch.demo.base.BaseService;
import cn.sunzq.springdata.elasticsearch.demo.document.TempLincolnshireDocument;
import cn.sunzq.springdata.elasticsearch.demo.document.TempLincolnshireShortDocument;
import cn.sunzq.springdata.elasticsearch.demo.entity.test.TempLincolnshire;
import cn.sunzq.springdata.elasticsearch.demo.entity.test.TempLincolnshireShort;
import cn.sunzq.springdata.elasticsearch.demo.importdata.service.ImortService;
import cn.sunzq.springdata.elasticsearch.demo.service.test.ITempLincolnshireService;
import cn.sunzq.springdata.elasticsearch.demo.service.test.ITempLincolnshireShortService;
import cn.sunzq.springdata.elasticsearch.demo.utils.ElasticsearchOperationstUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.IndexQuery;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

/**
 * @创建人 孙中泉
 * @创建时间 2021/7/19
 * @描述
 */
@Service
public class ImortServiceImpl extends BaseService implements ImortService {

    @Autowired
    private ITempLincolnshireShortService tempLincolnshireShortService;

    @Autowired
    private ITempLincolnshireService tempLincolnshireService;

    @Autowired
    private ElasticsearchOperationstUtil elasticsearchOperationstUtil;

    @Override
    public boolean importDataToEs(String indexName) {
        LOGGER.info("indexName:" + indexName);
        if(indexName.equals("temp_lincolnshire_short")){
            List<TempLincolnshireShort> list = tempLincolnshireShortService.list();
            LOGGER.info("list size:" + list.size());
            elasticsearchOperationstUtil.createIndexIfNotExists(indexName , TempLincolnshireShortDocument.class);
            List<IndexQuery> queries = new LinkedList<>();
            list.stream().forEach( entity -> {
                IndexQuery indexQuery = new IndexQuery();
                indexQuery.setId(entity.getId()+"");
                indexQuery.setSource(elasticsearchOperationstUtil.toJSONString(entity));
                queries.add(indexQuery);
            });
            elasticsearchOperationstUtil.bulkIndex(queries, indexName);
            elasticsearchOperations.indexOps(IndexCoordinates.of(indexName)).refresh();
        }else if(indexName.equals("temp_lincolnshire")){
            elasticsearchOperationstUtil.createIndexIfNotExists(indexName , TempLincolnshireDocument.class);
            int id = 0;
            while (true){
                QueryWrapper queryWrapper = new QueryWrapper();
                queryWrapper.gt("id", id);
                queryWrapper.orderByAsc("id");
                queryWrapper.last("limit 100000");
                List<TempLincolnshire> list = tempLincolnshireService.list(queryWrapper);
                if(list == null || list.isEmpty()){
                    LOGGER.info("list size:0, id:" + id);
                    break;
                }else {
                    LOGGER.info("list size:" + list.size() + ", id:" + id);
                    List<IndexQuery> queries = new LinkedList<>();
                    list.stream().forEach( entity -> {
                        IndexQuery indexQuery = new IndexQuery();
                        indexQuery.setId(entity.getId()+"");
                        indexQuery.setSource(elasticsearchOperationstUtil.toJSONString(entity));
                        queries.add(indexQuery);
                    });
                    elasticsearchOperationstUtil.bulkIndex(queries, indexName);
                    elasticsearchOperations.indexOps(IndexCoordinates.of(indexName)).refresh();
                    id = list.get(list.size() - 1).getId();
                }
            }
        }
        return true;
    }
}
